package routers

import (
	"LoginTemplate/controllers"
	"LoginTemplate/utils"
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

func init() {
	beego.Router("/", &controllers.BaseController{}, "get:IndexHtml")
	beego.Router("/user/login", &controllers.UserController{}, "post:Login")
	//beego.Router("/department/update1", &controllers.RoleController{}, "post:SelectDepartment")
	beego.Router("/payment/callback", &controllers.WXController{}, "post:PaymentCallback")
	adminRouter := beego.NewNamespace("/admin",
		//查询用户信息
		beego.NSRouter("/user/my_info", &controllers.UserController{}, "post:MyInfo"),
		//添加用户
		beego.NSRouter("/user/save", &controllers.UserController{}, "post:SaveUser"),
		//查询用户
		beego.NSRouter("/user/list", &controllers.UserController{}, "post:UserList"),
		//更新个人信息
		beego.NSRouter("/user/save_myinfo", &controllers.UserController{}, "post:SaveMyinfo"),
		//修改密码
		beego.NSRouter("/user/password", &controllers.UserController{}, "post:Password"),
		//添加部门
		beego.NSRouter("/department/insert", &controllers.DepartmentController{}, "post:InsertDepartment"),
		//修改部门
		beego.NSRouter("/department/update", &controllers.DepartmentController{}, "post:UpdateDepartment"),
		//删除部门
		beego.NSRouter("/department/delete", &controllers.DepartmentController{}, "post:DeleteDepartment"),
		//	查询部门
		beego.NSRouter("/department/selectPage", &controllers.DepartmentController{}, "post:SelectDepartment"),
		//添加角色
		beego.NSRouter("/role/insert", &controllers.RoleController{}, "post:InsertRole"),
		//修改角色
		beego.NSRouter("/role/update", &controllers.RoleController{}, "post:UpdateRole"),
		//删除角色
		beego.NSRouter("/role/delete", &controllers.RoleController{}, "post:DeleteRole"),
		//	查询角色
		beego.NSRouter("/role/selectPage", &controllers.RoleController{}, "post:SelectRole"),
		//	添加门店
		beego.NSRouter("/shop/insert", &controllers.ShopController{}, "post:InsertShop"),
		//	修改门店
		beego.NSRouter("/shop/update", &controllers.ShopController{}, "post:UpdateShop"),
		//	删除门店
		beego.NSRouter("/shop/delete", &controllers.ShopController{}, "post:DeleteShop"),
		//	查询门店
		beego.NSRouter("/shop/selectPage", &controllers.ShopController{}, "post:SelectShop"),
		//	添加配置
		beego.NSRouter("/config/insert", &controllers.ConfigController{}, "post:InsertConfig"),
		//	修改配置
		beego.NSRouter("/config/update", &controllers.ConfigController{}, "post:UpdateConfig"),
		//	查询配置
		beego.NSRouter("/config/selectPage", &controllers.ConfigController{}, "post:SelectConfig"),
		//	删除配置
		beego.NSRouter("/config/delete", &controllers.ConfigController{}, "post:DeleteCofig"),
		//	添加分类
		beego.NSRouter("/classify/insert", &controllers.ClassifyController{}, "post:InsertClassify"),
		//	修改分类
		beego.NSRouter("/classify/update", &controllers.ClassifyController{}, "post:UpdateClassify"),
		//	查询分类
		beego.NSRouter("/classify/selectPage", &controllers.ClassifyController{}, "post:SelectClassify"),
		//	添加文章
		beego.NSRouter("/article/insert", &controllers.ArticleController{}, "post:InsertArticle"),
		//	修改文章
		beego.NSRouter("/article/update", &controllers.ArticleController{}, "post:UpdateArticle"),
		//	查询文章
		beego.NSRouter("/article/selectPage", &controllers.ArticleController{}, "post:SelectArticle"),
		//	添加社区
		beego.NSRouter("/community/insert", &controllers.CommunityController{}, "post:InsertCommunity"),
		//	修改社区
		beego.NSRouter("/community/update", &controllers.CommunityController{}, "post:UpdateCommunity"),
		//	查询社区
		beego.NSRouter("/community/selectPage", &controllers.CommunityController{}, "post:SelectCommunity"),
		//	添加客户
		beego.NSRouter("/client/insert", &controllers.ClientController{}, "post:InsertClient"),
		//	修改客户
		beego.NSRouter("/client/update", &controllers.ClientController{}, "post:UpdateClient"),
		//	查询客户
		beego.NSRouter("/client/selectPage", &controllers.ClientController{}, "post:SelectClient"),
		//	查询缴费记录
		beego.NSRouter("/paymentRecords/selectPage", &controllers.PaymentRecordsController{}, "post:SelectPaymentRecords"),
		//	微信支付
		beego.NSRouter("/wx/payment", &controllers.WXController{}, "post:WXPayment"),
		//	退款
		beego.NSRouter("/wx/refund", &controllers.WXController{}, "post:Refund"),
		//	添加销售代码
		beego.NSRouter("/market/insert", &controllers.MarketController{}, "post:InsertMarket"),
		//	修改销售代码
		beego.NSRouter("/market/update", &controllers.MarketController{}, "post:UpdateMarket"),
		//	查询销售代码
		beego.NSRouter("/market/selectPage", &controllers.MarketController{}, "post:SelectMarket"),
		//	添加商品
		beego.NSRouter("/commodity/insert", &controllers.CommodityController{}, "post:InsertCommodity"),
		//	修改商品
		beego.NSRouter("/commodity/update", &controllers.CommodityController{}, "post:UpdateCommodity"),
		//	查询商品
		beego.NSRouter("/commodity/selectPage", &controllers.CommodityController{}, "post:SelectCommodity"),
		//	查询日记账
		beego.NSRouter("/daybook/selectPage", &controllers.DataBookController{}, "post:SelectDayBook"),
		//	添加日报
		beego.NSRouter("/daily/insert", &controllers.DailyController{}, "post:InsertDaily"),
		//	查询日报
		beego.NSRouter("/daily/selectPage", &controllers.DailyController{}, "post:SelectDaily"),
		//	添加每日次数
		beego.NSRouter("/configurationfiles/insert", &controllers.ConfigurationFilesController{}, "post:InsertConfigurationFiles"),
		//	查询每日次数
		beego.NSRouter("/configurationfiles/selectPage", &controllers.ConfigurationFilesController{}, "post:SelectConfigurationFiles"),
		//	修改每日次数
		beego.NSRouter("/configurationfiles/update", &controllers.ConfigurationFilesController{}, "post:UpdateConfigurationFiles"),
		//	添加拜访记录
		beego.NSRouter("/payVisit/insert", &controllers.PayVisitController{}, "post:InsertPayVisit"),
		//	查询拜访记录
		beego.NSRouter("/payVisit/selectPage", &controllers.PayVisitController{}, "post:SelectPayVisit"),
	)
	beego.InsertFilter("/admin/*", beego.BeforeRouter, func(ctx *context.Context) {
		token := ctx.Input.Header("Authorization")
		if token != "" {
			user, err := utils.VerifyToken(token)
			if err == nil {
				controllers.GlobalUserInfo = &user
			} else {
				message401 := map[string]interface{}{
					"code":    401,
					"data":    "",
					"message": "token验证失败",
				}
				message401byte, _ := json.Marshal(message401)
				ctx.Output.Body(message401byte)
				return
			}
		} else {
			message401 := map[string]interface{}{
				"code":    401,
				"data":    "",
				"message": "token验证失败",
			}
			message401byte, _ := json.Marshal(message401)
			ctx.Output.Body(message401byte)
			return
		}
	})
	userRouter := beego.NewNamespace("/client",
		//	查询社区
		beego.NSRouter("/community/selectPage", &controllers.CommunityController{}, "post:SelectCommunity"),
		//	查询客户
		beego.NSRouter("/client/selectPage", &controllers.ClientController{}, "post:SelectClient"),
		//	查询客户人数
		beego.NSRouter("/client/selectMyClientSum", &controllers.ClientController{}, "post:SelectMyClientSum"),
		//	查询欠款客户人数
		beego.NSRouter("/client/selectMyDebtClientSum", &controllers.ClientController{}, "post:SelectMyDebtClientSum"),
		//	查询公海
		beego.NSRouter("/client/selectMareLiBeRum", &controllers.ClientController{}, "post:SelectMareLiBeRum"),
	)
	beego.InsertFilter("/client/*", beego.BeforeRouter, func(ctx *context.Context) {
		token := ctx.Input.Header("Authorization")
		if token != "" {
			user, err := utils.VerifyUserToken(token)
			if err == nil {
				controllers.GlobalUserInfo1 = &user
			} else {
				message401 := map[string]interface{}{
					"code":    401,
					"data":    "",
					"message": "token验证失败",
				}
				message401byte, _ := json.Marshal(message401)
				ctx.Output.Body(message401byte)
				return
			}
		} else {
			message401 := map[string]interface{}{
				"code":    401,
				"data":    "",
				"message": "token验证失败",
			}
			message401byte, _ := json.Marshal(message401)
			ctx.Output.Body(message401byte)
			return
		}
	})
	beego.AddNamespace(userRouter)
	beego.AddNamespace(adminRouter)
}
